#Boa:Dialog:Dialog1

import wx
import wx.grid
import consultasBD
import Mensaje
import utiles
def create(parent):
    return Dialog1(parent)

[wxID_DIALOG1, wxID_DIALOG1GRID1, wxID_DIALOG1GUARDAR, 
 wxID_DIALOG1STATICTEXT1, wxID_DIALOG1STATICTEXT2, 
] = [wx.NewId() for _init_ctrls in range(5)]

class Dialog1(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DIALOG1, name='', parent=prnt,
              pos=wx.Point(359, 184), size=wx.Size(637, 377),
              style=wx.DEFAULT_DIALOG_STYLE, title='Acreditaciones de Cheques')
        self.SetClientSize(wx.Size(621, 341))

        self.grid1 = wx.grid.Grid(id=wxID_DIALOG1GRID1, name='grid1',
              parent=self, pos=wx.Point(8, 48), size=wx.Size(592, 232),
              style=0)

        self.staticText1 = wx.StaticText(id=wxID_DIALOG1STATICTEXT1,
              label='Listado de Cheques SIN ACREDITAR:', name='staticText1',
              parent=self, pos=wx.Point(8, 8), size=wx.Size(180, 13), style=0)

        self.staticText2 = wx.StaticText(id=wxID_DIALOG1STATICTEXT2,
              label='Colocar una X en la ultima columna , en la fila donde se encuentre el cheque acreditado',
              name='staticText2', parent=self, pos=wx.Point(8, 24),
              size=wx.Size(418, 13), style=0)

        self.guardar = wx.Button(id=wxID_DIALOG1GUARDAR,
              label='Guardar Acreditaciones', name='guardar', parent=self,
              pos=wx.Point(456, 296), size=wx.Size(144, 32), style=0)
        self.guardar.Bind(wx.EVT_BUTTON, self.OnGuardarButton,
              id=wxID_DIALOG1GUARDAR)

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.cargaGrilla()
    
    def cargaGrilla(self):
        self.grid1.CreateGrid(1000,6)
        columnas =['Nro Cheque','Fecha Emision','Fecha Cobro','Importe','Destinatario','Acreditado']
        for i in range(len(columnas)-1,-1,-1): 
            self.grid1.SetColLabelValue(i, columnas[i])
        self.grid1.AutoSizeColumn(0,True)
        self.grid1.AutoSizeRow(0,True)
        self.grid1.AutoSizeColumn(2,True)
        self.grid1.AutoSizeColumn(3,True)
        self.grid1.AutoSizeColumn(4,True)
        self.grid1.AutoSizeColumn(5,True)
        self.grid1.DisableDragColSize()
        self.grid1.DisableDragRowSize()
        self.total = "" 
        for x in xrange(1000):
            for i in xrange(6):
                if i!=5:
                    self.grid1.SetReadOnly(x,i,True)
        
        SQL = "select * from bancos where acreditado='' order by chequenro asc"
        resultados = consultasBD.ordenCrudaDict(SQL)
        
        
        for i,r in enumerate(resultados):
                print ( str(r["fechaemision"]))
                print str(r["chequenro"])
                self.grid1.SetCellValue(i, 0, str(r["chequenro"])) 
                self.grid1.SetCellValue(i, 1, str(r["fechaemision"])) 
                self.grid1.SetCellValue(i, 2, str(r["fechacobro"])) 
                self.grid1.SetCellValue(i, 3, str(r["importe"]))
                self.grid1.SetCellValue(i, 4, str(r["destinatario"]))
                self.grid1.SetCellValue(i, 5, "") 

    def OnGuardarButton(self, event):
        for i in xrange(1000):
            if (self.grid1.GetCellValue(i,5)=="x"):
                tablaSQL = "bancos"
                camposSQL = "acreditado = 'x'"
                condicionSQL = "chequenro = '"+str(self.grid1.GetCellValue(i,0))+"'"
                consultasBD.executaUpdateSQL(None,tablaSQL,camposSQL,condicionSQL)
                mensaje = "SE SEALIZO LA ACREDITACIN DEL CHEQUE " + str(self.grid1.GetCellValue(i,0))
                ventana = Mensaje.Mensaje(self,mensaje)
                try:
                    ventana.ShowModal()
                finally:
                    ventana.Destroy()
                
        self.Close()
